/*=========================================*
*             I N C L U D E S             *
*=========================================*/
#include <string.h>
#include <stdio.h>
#include <ctosapi.h>
#include <stdlib.h>
#include <stdarg.h>

/*==========================================*
 *            D E F I N E S                 *
 *==========================================*/
#define d_BUFF_SIZE   32

//define const "CASTLES" Graphic Logo //
const BYTE baLCDLogo[]={ //Width=46, Height=5
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xC0,0x40,0x40,0x60,0x20,0x20,0x20,0x20,
	0x20,0x20,0x60,0x60,0x40,0xC0,0x80,0x00,0x00,0x00,0x00,0x80,0xE0,0x70,0xC0,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

	0x00,0x00,0xC0,0x78,0x0C,0x86,0xE3,0x31,0x08,0x84,0xE6,0xE2,0x73,0x73,0x71,0x71,
	0xF1,0xF3,0xE2,0xE2,0xC4,0x8C,0x18,0xF1,0x73,0x1E,0x06,0xC3,0x70,0x30,0x60,0x83,
	0x0E,0x18,0x60,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

	0x00,0x00,0x3F,0xE0,0x00,0x1F,0x70,0xC0,0x00,0x1F,0x7F,0xFF,0xFC,0xF8,0xF8,0xFC,
	0xFF,0xFF,0xFF,0x7F,0x3F,0x0F,0x87,0xC1,0x30,0x0C,0x87,0xE1,0x38,0x18,0x70,0xC1,
	0x07,0x0C,0x30,0xE1,0x83,0x0C,0x38,0xE0,0x80,0x00,0x00,0x00,0x00,0x00,

	0x00,0x00,0x00,0x01,0x03,0x06,0x0C,0x19,0x33,0x22,0x64,0x44,0x48,0x48,0x49,0x49,
	0x49,0x48,0x4C,0x64,0x26,0x23,0x11,0x18,0x0C,0x06,0x03,0x00,0x00,0x00,0x00,0x00,
	0x03,0x0E,0x18,0x70,0x03,0x06,0x1C,0x30,0x41,0x07,0x1C,0x30,0x00,0x00,

	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};

//define const "CASTLES" Chinese Logo //
const BYTE baLCDLogo_1[]={ //Width=128, Height=5
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0xC0,0x80,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0xC0,0x80,0x00,0x80,0x80,0x80,0x80,
	0x80,0x80,0xC0,0xC0,0xC0,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0xC0,0xC0,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0xC0,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,
	0xC0,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

	0x00,0x00,0x00,0x00,0x40,0xC0,0xC0,0x40,0xE7,0xFF,0xFF,0x20,0xE0,0xE0,0x60,0x00,
	0x80,0x80,0xC0,0xC0,0xC0,0xC0,0xC0,0x40,0x60,0x60,0x40,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x80,0x60,0x30,0x38,0xFE,0xF7,0x03,0x40,0x40,0xC0,0x4F,0x4F,0x48,
	0xF8,0xFC,0x6C,0x6F,0x67,0x61,0x60,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x08,0x08,0x8C,0x8C,0xFE,0xFF,0xD3,0xC3,0xC1,0x80,0x40,0xC4,
	0xCC,0xDC,0x08,0x00,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x40,0x40,0x40,0x40,0x63,0xFF,0x77,0x20,0x20,0x20,0x20,0x20,0x30,0x30,
	0xFF,0xFF,0x10,0x10,0x18,0x18,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

	0x00,0x00,0x00,0x00,0x00,0x0F,0x0F,0x08,0xFF,0xFF,0xCF,0x84,0x77,0x77,0xE0,0xC0,
	0x00,0x00,0x00,0x01,0xFF,0xFF,0x00,0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x3F,0x1F,0x00,0x08,0x0C,0x06,0xF7,0xF3,0xE0,
	0x8F,0x9F,0x80,0x80,0x01,0x03,0x06,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x01,0x83,0xC1,0x71,0x39,0x1F,0xFF,0xFF,0x0C,0x18,0x18,0x04,0x04,0x04,
	0x04,0x04,0x06,0x06,0xFF,0xFF,0x07,0x02,0x03,0x02,0x02,0x00,0x00,0x00,0x00,0x00,
	0x40,0xC0,0xE0,0x60,0x30,0x30,0xF8,0xFF,0x06,0x02,0x00,0x00,0x04,0x1C,0x34,0x66,
	0xC7,0x83,0xF2,0x7E,0x1F,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

	0x00,0x00,0x00,0x02,0x06,0x07,0x03,0x03,0x01,0x01,0x00,0x00,0x00,0x02,0x02,0x02,
	0x02,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x10,0x30,0x30,0x30,0x31,0x11,0x11,0x11,0x1B,0x1F,0x1F,
	0x19,0x19,0x19,0x18,0x18,0x18,0x18,0x18,0x18,0x10,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x01,0x01,0x00,0x00,0x00,0x1E,0x3F,0x1F,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x3F,0x1F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x0C,0x1C,0x1F,0x1F,0x00,0x00,0x10,0x10,0x18,0x08,0x0C,0x06,
	0x07,0x07,0x0F,0x1E,0x1C,0x3C,0x38,0x38,0x10,0x10,0x00,0x00,0x00,0x00,0x00,0x00,

	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	};

/* ==========================================================================
 *
 * FUNCTION NAME: main
 *
 * DESCRIPTION:   function main is responsible for all the tasks listed in the
 *                introduction to this demo.
 *
 * RETURN:        none.
 *
 * NOTES:         none.
 *
 * ========================================================================== */
int main(int argc, char *argv[])
{
  //Declare Variable //
  BYTE key;
  USHORT ret;
  int i;
  BYTE babuff[d_BUFF_SIZE];

  //sysheap_setup ( 256 * 1024 ); // initial newlib and create 256K heap

  // TODO: Add your program here //
    
  CTOS_LCDTSelectFontSize(0x101E);
  
  //Set the LCD Display is Graphic Mode //
  CTOS_LCDSelectMode(d_LCD_GRAPHIC_MODE);
  
  //Clear Canvas //
  CTOS_LCDGClearCanvas();
  
  //Draw a pixel on the canvas //
  for (i=0; i<=127; i++){
    CTOS_LCDGPixel(i,10,TRUE);	//Draw a horizontal line
    CTOS_LCDGPixel(20,i,TRUE);	//Draw a vertical line
  }
  
  //Draw a pattern on the canvas //
  CTOS_LCDGShowPic(80,0,(BYTE *)baLCDLogo,sizeof(baLCDLogo),46);  //Draw the "CASTLES" Graphic Logo //
  
  CTOS_LCDGSetBox(40,17,20,7,1);  //Draw a rectangle //
  CTOS_LCDGSetBox(45,43,60,7,1);  //Draw a rectangle //
  
  //Draw a string on the canvas //
  CTOS_LCDGTextOut(0,20,"CASTLES",d_LCD_FONT_8x16,FALSE);     //Draw the "CASTLES" string, Font size is 8x16, and no reverse //
  CTOS_LCDGTextOut(50,40,"Technology",d_LCD_FONT_8x8,FALSE);  //Draw the "Technology" string, Font size is 8x8 and no reverse//
  
  CTOS_Delay(3000); //Delay 3 second
  
  //Move the display window to display desire position of canvas //
  for (i=0; i<=20;i++){
    CTOS_LCDGMoveWindow(i); //Move 20 dot //
    CTOS_Delay(50); //Delay 50ms every dot //
  }
   
  CTOS_LCDGShowPic(0,30,(BYTE *)baLCDLogo_1,sizeof(baLCDLogo_1),128); //Draw the "CASTLES" Chinese Pattern Logo //
  CTOS_LCDGTextOut(0,20,"CASTLES",d_LCD_FONT_8x16,FALSE); //Draw the "CASTLES" string, Font size is 8x16, and no reverse //

  //Get the current position of window //
  ret = CTOS_LCDGGetWindowOffset(); //ret = retrun value of offset of the current window //
  
  CTOS_Delay(3000);
  for (i=ret; i>=0;i--){
    CTOS_LCDGMoveWindow(i);
    CTOS_Delay(50);
  }
  
  CTOS_Delay(3000);
  CTOS_LCDGSetBox(0,0,128,64,1);
  
  for (i=0; i<=127; i++){
    CTOS_LCDGPixel(i,10,FALSE);
    CTOS_LCDGPixel(20,i,FALSE);
  }  
  CTOS_LCDGTextOut(0,20,"CASTLES",d_LCD_FONT_8x16,TRUE);
  CTOS_LCDGTextOut(50,40,"Technology",d_LCD_FONT_8x8,TRUE);
  
  for (i=0; i<=20;i++){
    CTOS_LCDGMoveWindow(i);
    CTOS_Delay(50);
  }

  CTOS_Delay(3000);
  
  CTOS_LCDGClearWindow();
  
  ret = CTOS_LCDGGetWindowOffset();
  
  CTOS_LCDGShowPic(80,0,(BYTE *)baLCDLogo,sizeof(baLCDLogo),46);
  
  CTOS_Delay(3000);
  for (i=ret; i>=0;i--){
    CTOS_LCDGMoveWindow(i);
    CTOS_Delay(50);
  }
  
  CTOS_Delay(3000);
  
  //Set the LCD Display is Text Mode //
  CTOS_LCDSelectMode(d_LCD_TEXT_MODE);
  
  //Display a character at current cursor position //
  CTOS_LCDTPutch('C');
  CTOS_Delay(300);
  
  //Display a character at specific cursor position //
  CTOS_LCDTPutchXY(3,1,'A');
  CTOS_Delay(300);
  CTOS_LCDTPutchXY(5,1,'S');
  CTOS_Delay(300);
  CTOS_LCDTPutchXY(7,1,'T');
  CTOS_Delay(300);
  CTOS_LCDTPutchXY(9,1,'L');
  CTOS_Delay(300);
  CTOS_LCDTPutchXY(11,1,'E');
  CTOS_Delay(300);
  CTOS_LCDTPutchXY(13,1,'S');
  
  //Locate the cursor to specific coordinate //
  CTOS_LCDTGotoXY(1,2);
  
  CTOS_Delay(300);
  CTOS_LCDTPutch('T');
  CTOS_Delay(300);
  CTOS_LCDTPutchXY(2,2,'e');
  CTOS_Delay(300);
  CTOS_LCDTPutchXY(3,2,'c');
  CTOS_Delay(300);
  CTOS_LCDTPutchXY(4,2,'h');
  CTOS_Delay(300);
  CTOS_LCDTPutchXY(5,2,'n');
  CTOS_Delay(300);
  CTOS_LCDTPutchXY(6,2,'o');
  CTOS_Delay(300);
  CTOS_LCDTPutchXY(7,2,'l');
  CTOS_Delay(300);
  CTOS_LCDTPutchXY(8,2,'o');
  CTOS_Delay(300);
  CTOS_LCDTPutchXY(9,2,'g');
  CTOS_Delay(300);
  CTOS_LCDTPutchXY(10,2,'y');
  
  CTOS_Delay(500);
  
  //Set the reverse attribute of the character //
  CTOS_LCDTSetReverse(TRUE);  //the reverse enable //
  
  //Display a string at specific cursor position //
  CTOS_LCDTPrintXY(1,4,"Castles");  //Display the "Castles" string in the 4 line//
  CTOS_Delay(500);
  CTOS_LCDTPrintXY(5,5,"Technology"); //Display the "Technology" string in the 5 line//
  CTOS_LCDTSetReverse(FALSE); //the reverse disable //
  
  memset(babuff,0x00,sizeof(babuff));
  CTOS_Delay(500);
  
  //Return the current X cursor position //
  ret = CTOS_LCDTWhereX();
  sprintf(babuff,"X => %d",ret);
  CTOS_LCDTPrintXY(1,7,babuff);	//Display value of return the current X crusor position in the 7 line //
  
  //Return the current Y cursor position //
  ret = CTOS_LCDTWhereY();
  sprintf(babuff,"Y => %d",ret);
  CTOS_LCDTPrintXY(10,7,babuff);  //Display value of return the current Y crusor position in the 7 line //
  
  CTOS_Delay(2000);
  for (i=ret; i>=1; i--){
    CTOS_LCDTGotoXY(1,i);
    
    //Clear all characters displayed from the cursor position to the end of line.
    CTOS_LCDTClear2EOL();
    CTOS_Delay(500);
  }
  
  //Set the English (ASCII code) font size attribute of the character //
  CTOS_LCDTSelectFontSize(d_LCD_FONT_8x16);
  
  CTOS_LCDTSetReverse(TRUE);
  
  //Display a character at current cursor position //
  CTOS_LCDTPrint("CASTLES");  //Dispay the "CASTLES" string in currnet cursor position //
  CTOS_Delay(500);
  CTOS_LCDTPrintXY(1,2,"Technology");
  CTOS_LCDTSetReverse(FALSE);
  
  CTOS_Delay(500);
  CTOS_LCDTPrintXY(1,3,"Castles");
  CTOS_Delay(500);
  CTOS_LCDTPrintXY(5,4,"Technology");
  
  CTOS_Delay(2000);
  //Clear LCD display and reset the cursor //
  CTOS_LCDTClearDisplay();
  
  CTOS_LCDTSelectFontSize(d_LCD_FONT_12x24);  
  CTOS_LCDTPrintXY(1,1,"CASTLES");
  CTOS_Delay(500);
  CTOS_LCDTSetReverse(TRUE);
  CTOS_LCDTPrintXY(1,2,"Technology");
  CTOS_LCDTSetReverse(FALSE);
  CTOS_Delay(3000);
  
  CTOS_LCDTSelectFontSize(d_LCD_FONT_8x16);
  CTOS_LCDTPrintXY(1,4,"Press Any Key..");
  
  //Set default font size //
  CTOS_LCDTSelectFontSize(d_LCD_FONT_8x8);
    
  CTOS_KBDGet ( &key );

  exit(0);
}
